package com.cgbg.config;


import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class TopicRabbitConfig {

    //绑定键
    public final static String man = "topic.man";
    public final static String woman = "topic.woman";

    /**
     * 声明队列
     *
     * @return
     */
    @Bean
    public Queue firstQueue() {
        return new Queue(TopicRabbitConfig.man, true);
    }

    /**
     * 声明队列
     *
     * @return
     */
    @Bean
    public Queue secondQueue() {
        return new Queue(TopicRabbitConfig.woman, true);
    }

    /**
     * 声明topic交换机
     *
     * @return
     */
    @Bean
    public TopicExchange topicExchange() {
        return new TopicExchange("topicExchange");
    }

    /**
     * 将firstQueue和topicExchange绑定,而且绑定的键值为topic.man
     * 这样只要是消息携带的路由键是topic.man,才会分发到该队列
     *
     * @return
     */
    @Bean
    public Binding topicBinding() {
        return BindingBuilder.bind(firstQueue()).to(topicExchange()).with(man);
    }


    /**
     * 两个测试方法都会进这个里面
     *
     * @return
     */
    @Bean
    Binding bindingExchangeMessage2() {
        return BindingBuilder.bind(secondQueue()).to(topicExchange()).with("topic.#");
    }


}
